
package com.tinem.platform.module.starter.jpa.repository.rbac;

import com.tinem.platform.module.starter.jpa.entity.rbac.RbacAuthorities;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @description TODO
 * @site https://fzh.cloud/
 * @company
 * @create 2021-6-11 11:33
 */
public interface RbacAuthoritiesRepository extends JpaRepository<RbacAuthorities, String> {

    /**
    select
	*
    from
            t_rbac_authorities
    where
    exists (
            select
		1
                from
                t_rbac_authorities_group_authorities
                where
                c_authorities = t_rbac_authorities.c_authorities
                and exists (
                select
                1
                from
                t_rbac_authorities_group_user
                where
                c_group_id = t_rbac_authorities_group_authorities.c_group_id
                and c_user_id = :userId ) )
    or exists (
            select
		1
                from
                t_rbac_user_authorities
                where
                c_authority = t_rbac_authorities.c_authorities
                and c_user_id = :userId )
     **/
    @Query(nativeQuery = true,value = "select\n" +
            "\t*\n" +
            "    from\n" +
            "            t_rbac_authorities\n" +
            "    where\n" +
            "    exists (\n" +
            "            select\n" +
            "\t\t1\n" +
            "                from\n" +
            "                t_rbac_authorities_group_authorities\n" +
            "                where\n" +
            "                c_authorities = t_rbac_authorities.c_authorities\n" +
            "                and exists (\n" +
            "                select\n" +
            "                1\n" +
            "                from\n" +
            "                t_rbac_authorities_group_user\n" +
            "                where\n" +
            "                c_group_id = t_rbac_authorities_group_authorities.c_group_id\n" +
            "                and c_user_id = :userId ) )\n" +
            "    or exists (\n" +
            "            select\n" +
            "\t\t1\n" +
            "                from\n" +
            "                t_rbac_user_authorities\n" +
            "                where\n" +
            "                c_authority = t_rbac_authorities.c_authorities\n" +
            "                and c_user_id = :userId )")
    List<RbacAuthorities> findByUser(@Param("userId") String userId);
}